import Cookies from 'js-cookie'
import api from "@/api"
import { getToken, setToken, removeToken } from '@/plugins/auth'


const state = () => ({
  //获取数据URL
  token:getToken(),
  username:'',
  userid:'',
  login:false,
})
const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_NAME:(state,user) => {
  	state.username = user;
  },
  SET_USERID:(state,id) =>{
  	state.userid = id;
  },
  SET_LOGIN:(state,status) => {
  	state.login = status;
  }

}
const actions = {
	login({state,commit},{model,action,userinfo,callback}){
		return api[model][action](userinfo).then(response =>{
			if(response){
				commit('SET_TOKEN',response.token);
				commit('SET_NAME',response.admin_name)
				commit('SET_USERID',response._id)
				setToken(response.token);
			}
	      if(typeof callback == 'function'){
	        // callback();
	      }

	    })
	},
	get_token(state){
		return state.token;
	}

}

export default {
  state,
  mutations,
  actions
}
